# -*- coding: utf-8 -*-
import scrapy
import re

import requests
from pyquery import PyQuery as pq

class GithubSpider(scrapy.Spider):
    name = 'github'
    allowed_domains = ['github.com']
    start_urls = ['https://github.com/login']

    # headers = {
    #     'Referer': 'https://github.com/',
    #     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36',
    #     'Host': 'github.com'
    # }
    # login_url = 'https://github.com/login'
    # post_url = 'https://github.com/session'
    # feed_url = 'https://github.com/dashboard-feed'
    # logined_url = 'https://github.com/settings/profile'
    #
    #
    # session = requests.Session()
    #
    # def get_ga_id(self):
    #     response = self.session.get(self.login_url, headers=self.headers)
    #     doc = pq(response.text)
    #     ga_id = doc('input[class="js-octo-ga-id-input"]').attr('value')
    #     print(ga_id)
    #     return ga_id

    def parse(self, response):
        commit = response.xpath('//input[@name="commit"]/@value').extract_first()
        utf8 = response.xpath('//input[@name="commit"]/@value').extract_first()
        authenticity_token = response.xpath('//input[@name="authenticity_token"]/@value').extract_first()
        ga_id = response.xpath('//input[@name="ga_id"]/@value').extract_first()
        webauthn_support = response.xpath('//input[@name="webauthn-support"]/@value').extract_first()
        webauthn_iuvpaa_support = response.xpath('//input[@name="webauthn-iuvpaa-support"]/@value').extract_first()
        timestamp= response.xpath('//input[@name="timestamp"]/@value').extract_first()
        timestamp_secret= response.xpath('//input[@name="timestamp_secret"]/@value').extract_first()
        postData=dict(
            commit = commit,
            utf8 = utf8,
            authenticity_token = authenticity_token,
            # ga_id = self.get_ga_id(),
            ga_id = ga_id,
            login = "Xiaobu-wang",
            password = "15334183661qqlm",
            timestamp = timestamp,
            timestamp_secret = timestamp_secret,
        )
        postData["webauthn-support"] = "supported"
        postData["webauthn-iuvpaa-support"] = "unsupported"
        print(postData)
        yield scrapy.FormRequest(
            "https://github.com/session",
            formdata=postData,
            callback=self.after_login,
        )

    def after_login(self,response):
        # with open("a.html","w") as f:
        #     f.write(response.body.decode())
        print(re.findall("Xiaobu-wang",response.body.decode()))


"""
问题1：ga_id无法解决哦（None）——解决了，后面再修改
注意1：
    除scrapy.FormRequest()之外，还可以：
        yield scrapy.Request(
                url,
                method="POST",
                body=,
            )
"""